package com.webbei.shoppingCar.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.taobao.api.ApiException;
import com.taobao.api.domain.Refund;
import com.taobao.api.request.RefundGetRequest;
import com.taobao.api.response.RefundGetResponse;
import com.wabbei.fenxiao.action.MyTaobaoUtil;
import com.webbei.myCustom.action.MyUtil;
import com.webbei.pojo.Customer;
import com.webbei.pojo.Customs;
import com.webbei.pojo.Orders;
import com.webbei.pojo.RefundOrder;
import com.webbei.shoppingCar.dao.shoppingCarDao;

public class shoppingCarDaoImpl implements shoppingCarDao{

	private SqlSession session;

	public SqlSession getSession() {
		return session;
	}

	public void setSession(SqlSession session) {
		this.session = session;
	}

	public Customer getCustomerByUserName(String userName) {
		return (Customer)session.selectOne("customer.getCustomerByUserName", userName);
	}

	public Customs getCustomsById(Long customId) {
		return (Customs) (session.selectList("cust.getCustomsById", customId).get(0));
	}

	public Orders getOrdersById(Long orderId) {
		return (Orders) (session.selectList("orders.getOrdersById", orderId).get(0));
	}

	public Integer getIndusByProductId(Integer id) {
		System.out.println("id:"+id);
		Integer i=(Integer) session.selectOne("indusconpro.getIndusByProductId", id);
		System.out.println(i);
		return i;
	}


	public void saveOrder(Orders o) {
		session.insert("orders.createOrders",o);
	}
	public void saveOrder1(Orders o) {
		System.out.println("SaveOrder\uFFFD\uFFFDID:"+o.getOrderId());
		session.insert("orders.createOrders1",o);
	}

	public Integer getOrderStateByOrderId(Long id) {
		return (Integer) session.selectOne("orders.getOrderStateByOrderId",id);
	}

	public String getBgPicByBgPicId(Integer id) {
		return (String) session.selectOne("cust.getBgPicByBgPicId",id);
	}

	@Override
	public Integer getCustomerCountAtLogin(Map<String, String> map) {
		// TODO Auto-generated method stub
		return (Integer) session.selectOne("customer.getCustomerCountAtLogin",map);
	}

	@Override
	public String getUserNameByEmail(String email) {
		// TODO Auto-generated method stub
		return (String) session.selectOne("customer.getUserNameByEmail",email);
	}

	@Override
	public String getUserPasswordByUserName(String username) {
		// TODO Auto-generated method stub
		return (String) session.selectOne("customer.getUserPasswordByUserName",username);
	}

	@Override
	public void changeUserPassword(Map map) {
		// TODO Auto-generated method stub
		session.update("customer.changeUserPassword",map);
	}

	@Override
	public Integer isHasRelatedThird(Map m1) {
		// TODO Auto-generated method stub
		return (Integer)session.selectOne("thirdparty.isHasRelatedThird",m1);
	}

	@Override
	public void updateCustomAtshop(Customs c) {
		// TODO Auto-generated method stub
		session.update("cust.updateCustomAtshop",c);
	}

	@Override
	public String getTypesNameByUrl(String url) {
		// TODO Auto-generated method stub
		return (String) session.selectOne("cartype.getTypesNameByUrl",url);
	}

	@Override
	public Integer checkUserPassword(Map m) {
		// TODO Auto-generated method stub
		return (Integer) session.selectOne("customer.checkUserPassword",m);
	}

	public Integer getIndusByProductName(String productName) {
		Integer integer=(Integer) session.selectOne("indusconpro.getIndusByProductName", productName);
		return integer;
	}

	@Override
	public void saveOrder2(Orders o) {
		// TODO Auto-generated method stub
		System.out.println("\u04AASAVE\uFFFD\uFFFDorder\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD"+o);
		session.insert("orders.createOrders2",o);
	}

	@Override
	public Integer getIndusByAlias(String name) {
		return (Integer) session.selectOne("indusconpro.getIndusByAlias", name);
	}

	@Override
	public Integer getProductidByAlias(String string) {
		// TODO Auto-generated method stub
		return (Integer) session.selectOne("indusconpro.getProductidByAlias", string);
	}

	@Override
	public Long getNewTid() {
		// TODO Auto-generated method stub
		return (Long) session.selectOne("orders.getNewTid");
	}

	@Override
	public void updateOrderStateOfTradeSellerShip(long parseLong) {
		// TODO Auto-generated method stub
		session.update("orders.updateOrderStateOfTradeSellerShip",parseLong);
	}

	@Override
	public void changeMemo(Long tradeid, String buyerMessage, String sellerMemo) {
		// TODO Auto-generated method stub
		Map map=new HashMap();
		map.put("id", tradeid);
		map.put("a", buyerMessage);
		map.put("b", sellerMemo);
		session.update("orders.changeMemo",map);
		System.out.println("\uFFFD\u07B8\u0133\u0279\uFFFD");
	}

	@Override
	public void changOrderStateToZero(long parseLong) {
		// TODO Auto-generated method stub
		session.update("orders.changOrderStateToZero",parseLong);
	}

	@Override
	public void saveOrder3(Orders c) {
		System.out.println("\u04AA\uFFFD\uFFFD\uFFFD\uFFFD\u0136\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD"+c);
		// TODO Auto-generated method stub
		session.insert("orders.createOrders3",c);
	}

	@Override
	public List<Customs> getCustomsByTradeid(Long tid) {
		// TODO Auto-generated method stub
		return session.selectList("cust.getCustomsByTradeid",tid);
	}

	@Override
	public void createCustomAtshop(Customs c) {
		// TODO Auto-generated method stub
		session.insert("cust.createCustom",c);
	}

	@Override
	public void testProcedure() {
		// TODO Auto-generated method stub
		System.out.println("\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD");
		List<Customer> clist=session.selectList("customer.testProcedure");
		System.out.println(clist);
	}

	@Override
	public void changOrderStateToFour(long parseLong) {
		// TODO Auto-generated method stub
		session.update("orders.changOrderStateToFour",parseLong);
	}

	@Override
	public void saveRefundOrder(String rid,String sessionKey){
		Long refundid = Long.parseLong(rid.trim());
		RefundGetRequest req=new RefundGetRequest();
		req.setFields("refund_id, alipay_no, tid, oid, buyer_nick, seller_nick, total_fee, status, created, refund_fee, good_status, has_good_return, payment, reason, desc, num_iid, title, price, num, good_return_time, company_name, sid, address, shipping_type, refund_remind_timeout");
		req.setRefundId(refundid);
		//String sessionKey = MyTaobaoUtil.getSessionKey(o.getSellername());
		try {
			RefundGetResponse response = MyTaobaoUtil.client.execute(req , sessionKey);
			Refund refund = response.getRefund();
			RefundOrder rOrder = new RefundOrder();
			rOrder.setRefundid(refundid);
			rOrder.setOrderid(refund.getOid());
			rOrder.setCreated(refund.getCreated());
			rOrder.setPayment(refund.getPayment());
			rOrder.setRefundFee(refund.getRefundFee());
			rOrder.setReason(refund.getReason());
			rOrder.setTitle(refund.getTitle());
			rOrder.setTotalFee(refund.getPrice());
			rOrder.setTradeid(refund.getTid());
			rOrder.setNum(refund.getNum());
			rOrder.setBuyerNick(refund.getBuyerNick());
			System.out.println("shopping..rOrder..."+rOrder);
			session.delete("refundOrder.deleteRefundOrder",refund.getRefundId());
			session.insert("refundOrder.saveRefundOrder",rOrder);
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void changOrderStateToThree(long parseLong) {
		// TODO Auto-generated method stub
		session.update("orders.changOrderStateToThree",parseLong);
	}

	@Override
	public void changOrderStateToOne(Long tid) {
		// TODO Auto-generated method stub
		session.update("orders.changOrderStateToOne",tid);
	}

	@Override
	public void addSellCount(Integer productIdByBgPic) {
		// TODO Auto-generated method stub
		session.update("Products.addSellCount",productIdByBgPic);
	}

	@Override
	public void subSellCount(Integer productIdByBgPic) {
		// TODO Auto-generated method stub
		session.update("Products.subSellCount",productIdByBgPic);
	}

}
